package com.simplelove.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.simplelove.entity.MenstrualCycle;
import com.simplelove.mapper.MenstrualCycleMapper;
import com.simplelove.service.MenstrualCycleService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 月经周期服务实现类
 */
@Service
public class MenstrualCycleServiceImpl extends ServiceImpl<MenstrualCycleMapper, MenstrualCycle> implements MenstrualCycleService {

    @Override
    public List<MenstrualCycle> syncMenstrualCycles(Long userId, List<MenstrualCycle> cycles) {
        for (MenstrualCycle cycle : cycles) {
            cycle.setUserId(userId);
            cycle.setUpdatedAt(LocalDateTime.now());
            
            if (cycle.getId() == null) {
                // 新增记录
                cycle.setCreatedAt(LocalDateTime.now());
                this.save(cycle);
            } else {
                // 更新记录
                this.updateById(cycle);
            }
        }
        return cycles;
    }

    @Override
    public List<MenstrualCycle> getUserMenstrualCycles(Long userId) {
        QueryWrapper<MenstrualCycle> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                   .orderByDesc("start_date");
        return this.list(queryWrapper);
    }
}
